package com.wuzihao.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

public class MainFrame extends JFrame {

    private Snake snake;
    private Timer timer;
    private JPanel jPanel;
    private Node food;

    public MainFrame() throws HeadlessException {
        initFrame();
        initGamePanel();
        //初始化蛇
        initSnake();
        //设置定时器，让蛇在定时时间内移动
        initTimer();
        //初始化食物
        initFood();
        //设置键盘监听时间
        setKeyListener();
    }

    private void setKeyListener() {
        addKeyListener(new KeyAdapter() {
            //当键盘按下时，会自动掉此方法
            @Override
            public void keyPressed(KeyEvent e) {
                //键盘中的一个键都有一个编号
                switch (e.getKeyCode()){

                    case KeyEvent.VK_UP: //上键
                        if(snake.getDirection()!=Direction.DOWN){
                            snake.setDirection(Direction.UP);
                        }
                        break;
                    case KeyEvent.VK_DOWN: //下键
                        if(snake.getDirection()!=Direction.UP){
                            snake.setDirection(Direction.DOWN);
                        }
                        break;
                    case KeyEvent.VK_LEFT: //左键
                        if(snake.getDirection()!=Direction.RIGHT){
                            snake.setDirection(Direction.LEFT);
                        }
                        break;
                    case KeyEvent.VK_RIGHT: //右键
                        if(snake.getDirection()!=Direction.LEFT){
                            snake.setDirection(Direction.RIGHT);
                        }
                        break;
                }
            }
        });
    }

    //初始化食物
    private void initFood() {
        food=new Node();
        food.random();
    }

    private void initTimer() {
        this.timer = new Timer();
        TimerTask timerTask = new TimerTask(){
            @Override
            public void run() {
                snake.move();
                //判断蛇头是否和食物重合
                Node head = snake.getBody().getFirst();
                if(head.getX()==food.getX()&&head.getY()==food.getY()){
                    snake.eat(food);
                    food.random();
                }

                //重新绘制棋盘
                jPanel.repaint();
            }
        };
        //每100毫秒，执行一次监听事件
        timer.scheduleAtFixedRate(timerTask,0,100);

    }

    private void initSnake() {
        this.snake = new Snake();
    }

    private void initGamePanel(){
        jPanel = new JPanel(){
            @Override
            public void paint(Graphics g) {
                //清空棋盘
                g.clearRect(0,0,600,600);

                //绘制横线
                for (int i = 0; i <= 40; i++) {
                    g.drawLine(0,i*15,600,i*15);
                }
                //绘制竖线
                for (int i = 0; i <= 40; i++) {
                    g.drawLine(i*15,0,i*15,600);
                }

                //绘制蛇
                LinkedList<Node> body = snake.getBody();
                for (Node node : body) {
                    g.fillRect(node.getX()*15,node.getY()*15,15,15);
                }
                //绘制食物
                g.fillRect(food.getX()*15,food.getY()*15,15,15);
            }
        };
        this.add(jPanel);
    }

    private void initFrame(){
        this.setSize(615,610);
        this.setLocation(250,10);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBackground(new Color(245, 206, 206));
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}
